package cn.zzu.Etrip.oneday.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.administrator.etrip.DbOpenHelper;
import com.example.administrator.etrip.R;

import java.util.HashMap;

public class oneday_database extends AppCompatActivity{
    ListView orderlist;
    DbOpenHelper dbhelper;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.orderlist);
        orderlist=findViewById(R.id.order_list);
        //调用构造数据库的方法，
        dbhelper=new DbOpenHelper(this);
        //Intent得到数据
        Intent intent=this.getIntent();
        String place=intent.getStringExtra("目的地");
        Integer singleprice=intent.getIntExtra("singleprice",0);
        Integer number=intent.getIntExtra("number",1);
        Log.d("test","∂d3333333333333333333333333333333333333333333333333333333333333##############################"+number);
        //防止从我的信息页面直接插入空值,从订单页面才能过来
        if(intent!=null&&place!=null){
            dbhelper.getReadableDatabase().execSQL("insert into information values(null ,?,?,?)", new String[] {place,String.valueOf(number),String.valueOf(singleprice) });
            Toast.makeText(oneday_database.this, "添加订单成功！"

                    , Toast.LENGTH_LONG).show();
        }
          //查询语句，第二个参数，要么是null要么是条件内容
        final Cursor cursor = dbhelper.getReadableDatabase().rawQuery(
                "select * from information ",null);
        //显示查询的数据
        view(cursor);

     orderlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final int position1=position;
                //点击动态删除

                AlertDialog.Builder delete = new AlertDialog.Builder(oneday_database.this);
                delete.setTitle("是否删除");
                delete.setMessage("请确认？");
                delete.setIcon(R.mipmap.a);
                delete.setNegativeButton("关闭",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                delete.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Cursor cursor = (Cursor) orderlist.getItemAtPosition(position1);
                                int i1=cursor.getInt(cursor.getColumnIndex("_id"));
                                dbhelper.getWritableDatabase().execSQL("delete from information where _id="+i1);
                                Cursor cursor1 = dbhelper.getReadableDatabase().rawQuery(
                                        "select * from information ",null);
                                view(cursor1);
                            }
                        });
                delete.show();
                Cursor cursor1 = dbhelper.getReadableDatabase().rawQuery(
                        "select * from information ",null);
            view(cursor1);

            }
        });
    }
    //数据显示方法
    public void view(Cursor cursor){
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                oneday_database.this,
                R.layout.order, cursor,
                new String[] { "_id","place", "number", "price" }
                , new int[] {R.id.order_id,R.id.order_name,R.id.order_number, R.id.order_singleprice },
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);  // ③在数据库改变时重新查询和更新界面
        // 显示数据
        orderlist.setAdapter(adapter);
    }
}
